#ifndef COURSE_LIB_H_
#define COURSE_LIB_H_

#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>

#include "course.h"
#include "course.cpp"

Course Add_Course(string s){
	string i_c_id, i_s_id, i_l_id, i_room, i_time, temp;
	short i_sem;
	int mark;
	Course S;
	
	//course id
	mark = s.find("::");
	i_c_id = s.substr(0,mark);
	cout<<i_c_id<<"\n";
	//subject id
	temp = s.substr(mark+2);
	mark = temp.find("::");
	i_s_id = temp.substr(0,mark);
	cout<<i_s_id<<"\n";
	//lecturer id
	temp = temp.substr(mark+2);
	mark = temp.find("::");
	i_l_id = temp.substr(0,mark);
	cout<<i_l_id<<"\n";
	//sem
	temp = temp.substr(mark+2);
	mark = temp.find("::");
	i_sem = atoi(temp.substr(0,mark).c_str());
	cout<<i_sem<<"\n";
	//room
	temp = temp.substr(mark+2);
	mark = temp.find("::");
	i_room = temp.substr(0,mark);
	cout<<i_room<<"\n";
	//time
	i_time = temp.substr(mark+2);
	cout<<i_time<<"\n";
	S = Course(i_c_id, i_s_id, i_l_id, i_sem, i_room, i_time);
	cout<<"testing";return S;
}


void ReadCourse(string _file, Course l[], int &numCourse){
	ifstream fin;
	fin.open(_file.c_str());
	string s;
	if(!fin.is_open()){
		cout<<"Cannot open file.\n";
		exit(1);
	}else{
		while (getline(fin, s)){
			l[numCourse] = Add_Course(s);
			numCourse++;
			cout<<l[numCourse].getInfo();
		}
		fin.close();
	}
               
}
/*
void WriteOut(string _file, Course l[], int &numberCourse){
               ofstream fout;
               fout.open(_file.c_str());
               for (int i=0 ; i<numberCourse ; i++){
                              fout<<l[i].get_id()<<":"<<l[i].get_fname()<<":"<<l[i].get_dob()<<":"<<l[i].get_addr()<<":"<<l[i].get_year()<<":"<<l[i].get_program()<<":"<<l[i].get_classname()<<endl;
               }
               fout.close();
}*/
void getTimePara(Course c, string &start, string &end, int &day){
	string t = c.getTime(), day_temp;
	int mark = 0;
	mark = t.find("-");
	start = t.substr(0,mark);
	
	t = t.substr(0,mark+1);
	mark = t.find("-");
	end = t.substr(0,mark);
	
	t = t.substr(0,mark+1);
	day = atoi(t.c_str());
}

void getSubjectsBySem(int sem, Course *inp, int size_i, Course *out, int &size_o){
	int i = 0, count = 0;
	for(i; i < size_i; i++){
		if(sem == inp[i].getSem()){
			out[count] = inp[i];
			count++;
		}
	}
	size_o = count;
}

int getHourFromString(string s){
	int mark = s.find(":");
	return atoi(s.substr(0,mark).c_str());
}

int getMinuteFromString(string s){
	int mark = s.find(":");
	return atoi(s.substr(mark, s.length()).c_str());
}

void swap(Course &a, Course &b){
	Course temp;
	temp = a;
	a = b;
	b = temp;
}

void sortSubjectByTime(Course *inp, int size_i){
	int i = 1, day, day_l;
	string start, end, start_l, end_l;
	Course c;
	for(i; i<size_i; i++)
	{
		getTimePara(inp[i], start, end, day);
		getTimePara(inp[i-1], start_l, end_l, day_l);
		if(day < day_l){
			swap(inp[i], inp[i-1]);
			i = 1;
			continue;
		}
		if(getHourFromString(start) < getHourFromString(start_l)){
			swap(inp[i], inp[i-1]);
			i = 1;
			continue;
		}
	}
}

void printCalendar(Course *inp, int size){
	int i = 0;
	for(i; i < size; i++){
		cout<<inp[i].getInfo();
	}
}

#endif